package cn.kgc.controller;


import cn.kgc.bean.pojo.EpAnswer;
import cn.kgc.bean.vo.EpAnswerVO;
import cn.kgc.bean.vo.Result;
import cn.kgc.service.EpAnswerService;
import cn.kgc.service.EpQuestionService;
import cn.kgc.service.EpStaffService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("epAnswer")
public class EpAnswerController {
    @Resource
    private EpAnswerService epAnswerService;

    @Resource
    private EpQuestionService epQuestionService;

    @Resource
    private EpStaffService epStaffService;

    @RequestMapping("/addAnswer")
    public Result addAnswer(@RequestBody EpAnswerVO epAnswerVO){
        Integer questionInfoId = epAnswerVO.getQuestionInfoId();
        String creator = epAnswerVO.getCreator();
        LocalDateTime localDateTime = LocalDateTime.now();
        try{
            for (int i = 0; i < epAnswerVO.getAnswers().length; i++) {
                EpAnswer epAnswer = new EpAnswer();
                epAnswer.setQuestionInfoId(questionInfoId);
                epAnswer.setQuestionId(i+1);
                epAnswer.setCreator(creator);
                epAnswer.setAnswer(epAnswerVO.getAnswers()[i].getContent());
                epAnswer.setCreateTime(localDateTime);
                epAnswerService.addAnswer(epAnswer);
            }
            List<String> correctAnswer = epQuestionService.getAllCorrectAnswerByQuestionInfoId(questionInfoId);
            List<String> answerByCreator = epAnswerService.getAllAnswerByQuestionInfoIdAndCreator(questionInfoId,creator);

            Integer score = 0; //员工培训得分
            for (int i = 0; i < correctAnswer.size(); i++) {
                if(correctAnswer.get(i).equals(answerByCreator.get(i))){
                    score += 10;
                }
            }
            epStaffService.updateScoreByStaffId(creator,score);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure();
        }
    }
}
